package org.funtester.performance.books.chapter10.section3;

import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.funtester.performance.books.chapter09.section3.UserBehavior;

/**
 * 键值池化工程类,用于创建用户行为对象
 */
public class KeyedUserBehaviorFactory extends BaseKeyedPooledObjectFactory<String, UserBehavior> {

    @Override
    public UserBehavior create(String key) throws Exception {
        return new UserBehavior(key);// 创建对象
    }

    @Override
    public PooledObject<UserBehavior> wrap(UserBehavior value) {
        return new DefaultPooledObject<>(value);// 包装对象
    }

    @Override
    public void passivateObject(String key, PooledObject<UserBehavior> p) throws Exception {
        p.getObject().clearKeepAction();// 清空对象
        super.passivateObject(key, p);// 调用父类方法
    }

}
